<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\ReservationImgs;
use App\Admin\Repositories\VillageThing;
use App\Models\SystemRegion;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\Models\User as PrevUser;
use Dcat\Admin\Controllers\AdminController;

class VillageThingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new VillageThing(), function (Grid $grid) {
            $region_id = Admin::user()->region_id;
            if ($region_id) {
                $grid->model()->where('region_id', $region_id);
                $grid->model()->orderBy('id', 'desc');
            } else {
                $grid->selector(function (Grid\Tools\Selector $selector) {
                    $system_regions = SystemRegion::pluck('region_name','id');
                    $system_regions[]  = '其他';
                    $selector->selectOne('region_id', '地区', $system_regions);
                });
            }

            $grid->column('id')->sortable();
            $grid->column('content')->width('200px')->limit(50);
            $grid->column('file', '图片、视频')
                ->if (function () {
                    return $this->imgs;
                })
                ->display('查看')
                ->expand(function(){
                    return $this->imgs ? ReservationImgs::make(['imgs' => $this->imgs]) : '';
                })
                ->else()
                ->display(function(){
                    $html = '';
                    if ($this->video) {
                        $html = '<a href="'.config('app.img_url') . $this->video.'" target="_blank"><i class="fa fa-video-camera"></i>  查看</a>';
                    }
                    return $html;
                });


            $grid->column('region_id')
                ->display(function($region_id){
                    $systemRegion = SystemRegion::find($region_id);
                    return  $systemRegion ? $systemRegion -> region_name : '';
                });
            $grid->column('user_id')->display(function($user_id){
                $user = PrevUser::find($user_id);
                return $user ? ($user -> real_name ?: $user -> user_name) : '';
            });

            $grid->column('is_village_admin_things', '乡村振兴员') ->display(function($is_village_admin_things){
                return $is_village_admin_things ? '是' : '否';
            });
            $grid->column('zan_num');
            $grid->column('comment_num');
            $grid->column('share_num');
            $grid->column('is_top') ->switch();
            $grid->column('weight');
            $grid->column('is_show') ->switch();
            $grid->column('created_at', '发布时间');
            $grid->column('comments','评论')->display(function () {
                if ($this->comment_num > 0) {
                    return '<a href="'. admin_base_path('village_things_comment?things_id='.$this->id) .'">查看</a>';
                } else {
                    return '';
                }
            });

            $grid->quickSearch(['content']) ->placeholder('搜索....');
            $grid->disableEditButton();
            $grid->disableCreateButton();
            $grid->disableViewButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new VillageThing(), function (Show $show) {
            $show->field('id');
            $show->field('content');

            $show->field('types')->as(function($types){
                return $types == 1 ? '多图' : '视频';
            });
            $show->field('imgs') ->image();
            $show->field('video');
            $show->field('region_id') ->as(function($region_id){
                $systemRegion = SystemRegion::find($region_id);
                return  $systemRegion ? $systemRegion -> region_name : '';
            });
            $show->field('user_id')->as(function($user_id){
                $user = PrevUser::find($user_id);
                return $user ? ($user -> real_name ?: $user -> user_name) : '';
            });
            $show->field('is_village_admin_things') ->as(function($is_village_admin_things){
                return $is_village_admin_things ? '乡村振兴员' : '普通用户';
            });
            $show->field('zan_num');
            $show->field('comment_num','评论数');
            $show->field('share_num');
            $show->field('is_top')->as(function($is_top){
                return $is_top ? '已置顶' : '未置顶';
            });
            $show->field('weight');
            $show->field('is_show')->as(function($is_show){
                return $is_show ? '展示' : '隐藏';
            });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new VillageThing(), function (Form $form) {
            $form->display('id');
            $form->text('content');
            $form->text('imgs');
            $form->text('video');
            $form->text('types');
            $form->text('region_id');
            $form->text('user_id');
            $form->text('is_village_admin_things');
            $form->text('zan_num');
            $form->text('comment_num');
            $form->text('share_num');
            $form->text('is_top');
            $form->text('weight');
            $form->text('is_show');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
